<?php
require_once (dirname(__FILE__)."/../config/config.php");
if($_POST['method']=='addSuser'){
	$activety['activity_code']=$_POST['activity_code'];
	$activety['first_name']=$_POST['first_name'];
	$activety['last_name']=$_POST['last_name'];
	$activety['email']=$_POST['email'];
	$activety['zipcode']=$_POST['zipcode'];
	$activety['job']=$_POST['job'];
	$activety['ip']=$_POST['ip'];
	$activety['partner_id']=$_POST['partner_id'];
	$activety['lang']=$_POST['lang'];
	//$sql="INSERT INTO `zy_survey_user`(`partner_id`,`activety_code`,`first_name`,`last_name`,`email`,`zipcode`,`job`,`create_time`) VALUES //(".$activety['partner_id'].",'".$activety['activity_code']."','".$activety['first_name']."','".$activety['last_name']."','".$activety['email']."','".$activety['zipcode']//."',".$activety['job'].",'".date('Y-m-d H:i:s')."')";

	//判断partner_id 是否为空
	if(empty($activety['partner_id'])){
		$querysql="select user_id from zy_activity where activity_code='".$activety['activity_code']."'";
		$rs = mysql_query($querysql);
		$p = mysql_fetch_array($rs);
		$activety['partner_id']=$p['user_id'];
	}

	$sql="INSERT INTO `zy_survey_user`(`partner_id`,`activety_code`,`first_name`,`last_name`,`email`,`zipcode`,`job`,`ip`,`lang`,`create_time`) VALUES ('".$activety['partner_id']."','".$activety['activity_code']."','".$activety['first_name']."','".$activety['last_name']."','".$activety['email']."','".$activety['zipcode']."','".$activety['job']."','".$activety['ip']."','".$activety['lang']."','".date('Y-m-d H:i:s')."')";

	if(mysql_query($sql)){
		echo "success";
	}else{
		echo "error";
	}
}else if($_POST['method']=='checkActivityCode'){
//检查活动代码是否存在,如果存在则返回partnerId
	$sql_f="select user_id,activity_out_time from zy_activity where activity_code='".$_POST['activity_code']."'";
	$result=mysql_query($sql_f);
	//查不到partner的账号
	if($a = mysql_fetch_array($result)){
		$partner_id=$a['user_id'];
		//活动存在的情况下
		//判断活动是否过期
		if(strtotime(date("Y-m-d"))>strtotime($a['activity_out_time'])){
			echo "due";
		}else{
			echo $partner_id;
		}
	}else{
		//活动不存在时
		echo "error";
	}
	exit;
}else if($_POST['method']=='checkEmail'){
	//判断该活动下邮箱是否存在
	$sql_f="select suid from zy_survey_user where email='".$_POST['email']."' and activety_code='".$_POST['activity_code']."'";
	$result=mysql_query($sql_f);
	if($a = mysql_fetch_array($result)){
		echo $a['suid'];
	}else{
		//email不存在
		echo "success";
	}
}else if($_POST['method']=='updateUser'){
	$activety['activity_code']=$_POST['activity_code'];
	$activety['email']=$_POST['email'];
	$activety['q1']=$_POST['q1'];
	$activety['mobile']=$_POST['mobile'];
	$address = $_POST['award_address_id'];
	$sql="update `zy_survey_user` set mobile='".$activety['mobile']."',favo='".$activety['q1']."',award_address_id='".$address."' where activety_code='".$activety['activity_code']."' and email='".$activety['email']."'";
	if(mysql_query($sql)){
		echo "success";
	}
}else if($_POST['method']=='info'){
	
	$activety['id_type']=$_POST['id_type'];
	$activety['birthday']=$_POST['birthday'];
	if($activety['id_type']==1){
		$activety['id_number']=$_POST['drive_state']."-".$_POST['id_number'];
	}else if($activety['id_type']==3){
		$activety['id_number']=$_POST['nationality']."-".$_POST['id_number'];
	}else{
		$activety['id_number']=$_POST['id_number'];
	}
	$activety['award_address_id']=$_POST['award_address_id'];
	$activety['suburb']=$_POST['suburb'];
	$activety['state']=$_POST['state'];
	$activety['full_street_address']=$_POST['full_street_address'];
	$activity['suid']=$_POST['suid'];

	$sql="update `zy_survey_user` set full_street_address='".$activety['full_street_address']."',state='".$activety['state']."', suburb='".$activety['suburb']."',award_address_id='".$activety['award_address_id']."', birthday='".$activety['birthday']."',id_type='".$activety['id_type']."', id_number='".$activety['id_number']."' where suid =".$activity['suid'];

	//$sql="update `zy_survey_user` set //id_type='".$activety['id_type']."',birthday='".$activety['birthday']."',id_number='".$activety['id_number']."',award_address_id='".$activety['award_address_id']."',suburb//='".$activety['suburb']."',state='".$activety['state']."' where suid=".$activety['suid'];
	if(!mysql_query($sql)){
		echo "error";
	}
	
	$sql_f="select * from zy_survey_user where suid='".$activity['suid']."'";

	$result_f=mysql_query($sql_f);
	if($a_f = mysql_fetch_array($result_f)){
		echo json_encode($a_f);
	}
}else if($_POST['method'] == 'queryAddress'){

	$address['code'] = $_POST['code'];

	//$sql="SELECT * FROM zy_survey_user LEFT JOIN zy_award_address ON zy_survey_user.`partner_id` = zy_award_address.`belong` WHERE zy_survey_user.suid='".$address['suid']."'";
	
	$sql="SELECT a.* FROM zy_award_address a LEFT JOIN `zy_activity` b ON a.belong =b.user_id WHERE b.activity_code='".$address['code']."'";

	$result=mysql_query($sql);
	while ($a_f=mysql_fetch_array($result)){
		$area[] = array($a_f['address_id'],$a_f['address']);
	}
	echo json_encode($area);
}else if($_POST['method'] == 'sendsms'){

	$sms['suid']=$_POST['suid'];
	
	$sql="SELECT zy_activity.activity_id,zy_survey_user.`partner_id`,zy_survey_user.`mobile`,zy_activity.activity_out_time,
			zy_survey_user.award_address_id,zy_award_address.`address` FROM zy_survey_user 
			LEFT JOIN zy_activity ON zy_survey_user.`activety_code`= zy_activity.`activity_code` 
			LEFT JOIN zy_award_address ON zy_survey_user.`award_address_id` = zy_award_address.`address_id`
			WHERE zy_survey_user.`suid` =".$sms['suid'];

	 $rs = mysql_query($sql);

	 $row = mysql_fetch_array($rs);

	 $password = getPassword($row['activity_id'],$row['partner_id']);

	$smsinfo = array('mobile'=>$row['mobile'],'activity_out_time'=>$row['activity_out_time'],'address'=>$row['address'],'password'=>$password,'userId'=>$row['partner_id'],'address_id'=>$row['award_address_id']);


	echo json_encode($smsinfo);


}else if($_POST['method'] == 'parameter'){
		$setParameter['mobile']=$_POST['mobile'];
		$setParameter['userId']=$_POST['userId'];
		$setParameter['password']=$_POST['password'];
		$setParameter['address_id']=$_POST['address_id'];
		$setParameter['suid']=$_POST['suid'];
		$sql="update zy_survey_user set `sms_status` = 1 where partner_id='".$setParameter['userId']."' and mobile='".$setParameter['mobile']."'";
		mysql_query($sql);
		$sqls="UPDATE `zy_gift_password` SET suser_id=".$setParameter['suid']." ,`state` = '1',`moblie` = '".$setParameter['mobile']."',`address` = '".$setParameter['address_id']."' WHERE `password` = '".$setParameter['password']."'";		
		mysql_query($sqls);
}else if($_POST['method']=='DuplicateSubmission'){
	//检查电话号码是否存在,如果存在说明已经提交

	if(isset($_POST['mobile'])){
		$sql="select mobile from zy_survey_user where mobile='".$_POST['mobile']."' and activety_code='".$_POST['activity_code']."'";
	}else if(isset($_POST['suid']) && isset($_POST['idnm'])){
		$sql="SELECT suser_id FROM zy_gift_password WHERE suser_id=".$_POST['suid'];
	}
	$result=mysql_query($sql);
	
	$rs = mysql_fetch_array($result);

	if(!empty($rs)){
		echo "cf";
	}else{
		echo "error";
	}
	exit();
}else if($_POST['method']=="set_award_user"){
	$activety['q1']=$_POST['q1'];
	$activety['mobile']=$_POST['mobile'];
	$activety['activity_code']=$_POST['activity_code'];
	$activety['id_type']=$_POST['id_type'];
	$activety['birthday']=$_POST['birthday'];
	if($activety['id_type']==1){
		$activety['id_number']=$_POST['drive_state']."-".$_POST['id_number'];
	}else if($activety['id_type']==3){
		$activety['id_number']=$_POST['nationality']."-".$_POST['id_number'];
	}else{
		$activety['id_number']=$_POST['id_number'];
	}
	$activety['award_address_id']=$_POST['award_address_id'];
	$activety['suburb']=$_POST['suburb'];
	$activety['state']=$_POST['state'];
	$activety['full_street_address']=$_POST['full_street_address'];
	$activity['suid']=$_POST['suid'];
	$sql="update `zy_survey_user` set mobile='".$activety['mobile']."',favo='".$activety['q1']."',full_street_address='".$activety['full_street_address']."',state='".$activety['state']."', suburb='".$activety['suburb']."',award_address_id='".$activety['award_address_id']."', birthday='".$activety['birthday']."',id_type='".$activety['id_type']."', id_number='".$activety['id_number']."' where suid =".$activity['suid'];
	mysql_query($sql);
	$sql_f="select * from zy_survey_user where suid='".$activity['suid']."'";
	$result_f=mysql_query($sql_f);
	if($a_f = mysql_fetch_array($result_f)){
		$password=getUpdatePassword($a_f,$activety['activity_code']);
		$a_f['password']=$password;
		echo json_encode($a_f);
	}
}

//随机从zy_gift_password表中拿出一个密码
function getPassword($activety_id,$user_id){
	$sqls="select password from zy_gift_password where activity_id='".$activety_id."' and suser_id='".$user_id."' and state= 0 limit 1";
	$rss = mysql_query($sqls);
	$rows = mysql_fetch_array($rss);
	//array_unique($rows);
	//$result = array_rand($rows,1);
	return $rows['password'];
}

/**
 * 先获取一个密码记录,根据获取的记录的ID,更新记录为已中奖已分配.
 * @param unknown $suid
 * @param unknown $user_id
 * @param unknown $moblie
 * @return Ambigous <>
 */
function getUpdatePassword($a_f,$activety_code){
	$sql="select activity_id from zy_activity where activity_code ='".$activety_code."'";
	$rs = mysql_query($sql);
	$row = mysql_fetch_array($rs);
	if(!empty($row)){
		$sql="select gift_password_id,password from zy_gift_password where  activity_id='".$row['activity_id']."' and state=0 limit 0,1";
		$rss = mysql_query($sql);
		$rows = mysql_fetch_array($rss);
		if(!empty($rows)){
			$sql="update zy_gift_password set suser_id='".$a_f['suid']."',state=1,moblie='".$a_f['mobile']."',`address` = '".$a_f['award_address_id']."' where gift_password_id=".$rows['gift_password_id'];
			$rs = mysql_query($sql);
			if($rs){
				return $rows['password'];
			}else{
				return "error";
			}
		}
	}
}
?>